package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.ISupplierService;
import cn.stylefeng.guns.dao.entity.RuleLevel;
import cn.stylefeng.guns.dao.entity.Supplier;
import cn.stylefeng.guns.modular.demo.controller.domain.*;
import cn.stylefeng.guns.modular.demo.excel.domain.SupplierExcelVO;
import cn.stylefeng.guns.modular.demo.excel.listener.SupplierListener;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@ApiResource(name = "示例", resBizType = ResBizTypeEnum.BUSINESS, path = "/supplier")
public class SupplierController {

    @Resource
    private ISupplierService supplierService;

    /**
     * 示例方法
     *
     * @author fengshuonan
     * @since 2021/1/24 10:59
     */
    @PostResource(name = "供应商列表", path = "/list", requiredLogin = true, requiredPermission = true)
    @ApiOperation("查询供应商列表")
    public ResponseData<PageResult<Supplier>> list(@RequestBody QuerySupplierRequest querySupplierRequest) {

        Page<Supplier> page = new Page<>();
        page.setCurrent(querySupplierRequest.getPageNo());
        page.setSize(querySupplierRequest.getPageSize());
        LambdaQueryWrapper<Supplier> lambdaQueryWrapper = Wrappers.<Supplier>lambdaQuery()
                .eq(StringUtils.isNotBlank(querySupplierRequest.getSupplierCode()), Supplier::getSupplierCode, querySupplierRequest.getSupplierCode())
                .like(StringUtils.isNotBlank(querySupplierRequest.getName()), Supplier::getName, querySupplierRequest.getName())
                .like(Objects.nonNull(querySupplierRequest.getInProvince()), Supplier::getInProvince, querySupplierRequest.getInProvince());
        Page<Supplier> page1 = supplierService.page(page, lambdaQueryWrapper);
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1));
    }


    /**
     * 供应商编辑
     *
     * @author fengshuonan
     * @since 2021/1/24 10:59
     */
    @PostResource(name = "供应商设置年返利", path = "/edit", requiredLogin = true, requiredPermission = true)
    @ApiOperation("供应商设置年返利")
    @BusinessLog
    public ResponseData<Boolean> set(@RequestBody SetSupplierRequest setSupplierRequest) {
        String supplierId = setSupplierRequest.getSupplierId();

        LambdaQueryWrapper<Supplier> lambdaQueryWrapper = Wrappers.<Supplier>lambdaQuery().eq(Supplier::getSupplierId, supplierId);
        Supplier supplier = supplierService.getOne(lambdaQueryWrapper);

        String address = setSupplierRequest.getAddress();
        if (StringUtils.isNotBlank(address)) {
            supplier.setAddress(address);
        }

        String name = setSupplierRequest.getName();
        if (StringUtils.isNotBlank(name)) {
            supplier.setName(name);
        }

        Integer inProvince = setSupplierRequest.getInProvince();
        if (Objects.nonNull(inProvince)) {
            supplier.setInProvince(inProvince);
        }

        List<SupplierRuleLevel> ruleLevelRequestList = setSupplierRequest.getRuleLevelList();
        if (CollectionUtils.isNotEmpty(ruleLevelRequestList)) {
            supplier.setRule(JSON.toJSONString(ruleLevelRequestList));
        }
        supplierService.updateById(supplier);
        return new SuccessResponseData<>(true);
    }


    @ResponseBody
    @PostResource(name = "供应商导入", path = "/upload", requiredLogin = true, requiredPermission = true)
    @ApiOperation("供应商导入")
    @BusinessLog
    public ResponseData<String> upload(MultipartFile file) throws IOException {
        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), SupplierExcelVO.class, new SupplierListener(supplierService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }


    @ResponseBody
    @PostResource(name = "供应商删除", path = "/delete", requiredLogin = true, requiredPermission = true)
    @ApiOperation("供应商删除")
    @BusinessLog
    public ResponseData<Boolean> delete(String id) throws IOException {
        supplierService.removeById(id);
        return new SuccessResponseData<>(true);
    }


    @ResponseBody
    @GetResource(name = "供应商详情", path = "/detail", requiredLogin = true, requiredPermission = true)
    @ApiOperation("供应商详情")
    public ResponseData<SupplierDetailVO> detail(String id) throws IOException {
        Supplier supplier = supplierService.getById(id);
        if (Objects.isNull(supplier)) {
            return new SuccessResponseData<>(null);
        }

        return new SuccessResponseData<>(conver(supplier));
    }

    SupplierDetailVO conver(Supplier supplier) {
        if (supplier == null) {
            return null;
        }
        SupplierDetailVO supplierDetailVO = new SupplierDetailVO();
        supplierDetailVO.setSupplierId(supplier.getSupplierId());
        supplierDetailVO.setSupplierCode(supplier.getSupplierCode());
        supplierDetailVO.setName(supplier.getName());
        supplierDetailVO.setAddress(supplier.getAddress());
        supplierDetailVO.setInProvince(supplier.getInProvince());
        supplierDetailVO.setRule(supplier.getRule());
        supplierDetailVO.setDelFlag(supplier.getDelFlag());
        supplierDetailVO.setCreateTime(supplier.getCreateTime());
        supplierDetailVO.setCreateUser(supplier.getCreateUser());
        supplierDetailVO.setUpdateTime(supplier.getUpdateTime());
        supplierDetailVO.setUpdateUser(supplier.getUpdateUser());
        String rule = supplier.getRule();
        if (StringUtils.isNotBlank(rule)) {
            supplierDetailVO.setRuleLevelList(JSON.parseArray(rule, RuleLevel.class));
        }
        return supplierDetailVO;
    }

}
